/*
 * @Copyright: EasyCompany 2021
 * @Author: asterisk
 * @Date: 2021-03-19 16:35:09
 * @Description: 
 */
import 'dart:io';

class GitInfoHelper {
  /// 获取当前项目git编译版本
  /// 1.读取当前[.git]文件夹里面[HEAD]文件内容，得到当前分支信息
  /// 2.根据分支文件，得到当前[commit id]
  /// ```
  /// @param currentDirectory 当前项目代码目录，如果不设置则使用[easy_log]项目自己的目录，其它项目需要手动指定目录
  /// ```
  static String getGitTag({Directory currentDirectory}) {
    Directory gitDir;
    if (null != currentDirectory) {
      gitDir = currentDirectory;
    } else {
      // gitDir = Directory.current;
      gitDir = Directory('./.git');
    }
    if (false == gitDir.existsSync()) {
      return '';
    }
    var gitHead = File('./.git/HEAD');
    if (false == gitHead.existsSync()) {
      return '';
    }
    var gitHeadContent = gitHead.readAsStringSync();
    if (false == gitHeadContent.startsWith(RegExp('ref:'))) {
      return '';
    }
    gitHeadContent = gitHeadContent.replaceFirst(RegExp('ref:'), '').trim();
    var gitCurrentBranch = File('./.git/' + gitHeadContent);
    if (false == gitCurrentBranch.existsSync()) {
      return '';
    }
    return gitCurrentBranch.readAsStringSync();
  }
}
